Home:ALL Converter>Get the character before another in a string in Python

Get the character before another in a string in Python

Ask Time:2021-11-02T21:26:41         Author:Swagrim

Json Formatter

The thing I am trying to achieve is that I want to get the character before another one in a string. For e.g, if the string is "a0c1e2g3", I want to get what's written in the index[0] by just having the index[1] like i want to get the value of the index[0] ("a") when I only have the value of index[1] ("1"). I want it for all characters. I want the string to be extendable too. I am using python 3.10.1

Expected result:

a
c
e
g

The result I could get till:

0
2
4
6

The code I tried:

s = "a0c1e2g3"
for c, i in enumerate(s):
    if i.isdigit():
        alpha_before_int = c-1
        print(alpha_before_int)
        # I want to get "a", "c", "e", "g"

Author:Swagrim,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69811279/get-the-character-before-another-in-a-string-in-python
Halley Oliveira :

>>> s = "a0c1e2g3"\n>>> for c, i in enumerate(s):\n... if i.isdigit():\n... alpha_before_int = s[c-1]\n... print(alpha_before_int)\n... # I want to get "a", "c", "e", "g"\n... \na\nc\ne\ng\n>>> \n",
2021-11-02T13:35:24
yy